/**********************************************************************************
 *  jcw - this is a environment for solving japan crosswords by users on computer *
 *  Copyright (C) 2008 by pluton <plutonpluton@mail.ru>                           *
 *                                                                                *
 *  This program is free software; you can redistribute it and/or modify          *
 *  it under the terms of the GNU General Public License as published by          *
 *  the Free Software Foundation; either version 2 of the License, or             *
 *  (at your option) any later version.                                           *
 *                                                                                *
 *  This program is distributed in the hope that it will be useful,               *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of                *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
 *  GNU General Public License for more details.                                  *
 *                                                                                *
 *  You should have received a copy of the GNU General Public License along       *
 *  with this program; if not, write to the Free Software Foundation, Inc.,       *
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                   *
 *********************************************************************************/

#include <QLabel>
#include <QBoxLayout>
#include <QDebug>
#include "palette.h"

Palette::Palette(paletteType t, QWidget* parent, Qt::WindowFlags f)
	: QWidget(parent, f), m_type(t)
{
	QVBoxLayout* layout = new QVBoxLayout;
	setLayout(layout);
	blocked = false;
}

Palette::~Palette()
{
}

void Palette::updatePixmap(quint16 c)
{
	pixmap = QPixmap(size());
	pixmap.fill(this, 0, 0);
	QPainter painter(&pixmap);
	painter.initFrom(this);
	painter.setPen(Qt::black);

	if (m_type == Layer)
	{
		if (c == 0)
		{
			QFont f;
			f.setBold(true);
			f.setPointSize(12);
			painter.setFont(f);
		}
		painter.drawText(10, 10, 40, 20, Qt::AlignCenter, tr("All"));
	}
	painter.drawRect(0, 0, 60, (m_cmCnt / 2 + ((m_cmCnt % 2 == 0) ? 0 : 1)) * 20 + 20 + shiftY);

	quint16 i;
	for (i = 0; i < m_cmCnt; i++)
	{
		if (i + shiftCol == c)
			continue;
		painter.setBrush(QColor(m_colorMap[i].red, m_colorMap[i].green, m_colorMap[i].blue));
		quint16 x = (i % 2 == 0) ? 10 : 30;
		quint16 y = i / 2 * 20 + 10 + shiftY;
		painter.drawRect(x, y, 20, 20);
	}
	if (c - shiftCol >= 0)
	{
		painter.setBrush(QColor(m_colorMap[c - shiftCol].red, m_colorMap[c - shiftCol].green, m_colorMap[c - shiftCol].blue));
		quint16 x = ((c - shiftCol) % 2 == 0) ? 7 : 27;
		quint16 y = (c - shiftCol) / 2 * 20 + 7 + shiftY;
		painter.drawRect(x, y, 26, 26);
	}

	update();
}

void Palette::paintEvent(QPaintEvent* /*event*/)
{
	QStylePainter painter(this);	//object for painting
	painter.drawPixmap(0, 0, pixmap);
}

void Palette::mousePressEvent(QMouseEvent* event)
{
	if (blocked)
		return;

	quint16 x = (event->pos().x() - 10) / 20;
	quint16 y = (event->pos().y() - 10 - shiftY) / 20;

	if (!(event->pos().x() - 10 < 0 || event->pos().y() - 10 - shiftY < 0 || x > 1 || y * 2 + x > m_cmCnt - 1))
	{
		emit currItemChanged(y * 2 + x + shiftCol);
		updatePixmap(y * 2 + x + shiftCol);
	}
	else if (event->pos().y() < 30 && m_type == Layer)
	{
		emit currItemChanged(0);
		updatePixmap(0);
	}
	event->accept();
}

void Palette::block()
{
	blocked = true;
}


void Palette::unblock()
{
	blocked = false;
}

void Palette::setColorMap(ColorCW::ColorMap* colorMap, quint16 cmCnt)
{
	m_colorMap = colorMap;
	m_cmCnt = cmCnt;

	shiftY = shiftCol = 0;
	if (m_type == Layer)
	{
		shiftY = 30;
		shiftCol = 1;
	}

	setMinimumWidth(61);
	setMinimumHeight((m_cmCnt / 2 + ((m_cmCnt % 2 == 0) ? 0 : 1)) * 20 + 1 + 20 + shiftY);

	updatePixmap(0);
}
